package com.fzj.example.collection;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Spliterator;

/**
 * ClassName: MySet
 * date: 2019/10/14 14:20
 * Description: TODO
 *
 * @author fanzj@bonree.com
 * @since JDK 1.8
 */
public class MySet {
    public static void main(String[] args) {
        HashSet<Integer> setDemo=new HashSet<>();
        //自动装箱
        setDemo.add(2);
        System.out.println("......add "+setDemo);
        //
        setDemo.add(3);
        //
        System.out.println("......add "+setDemo);
        setDemo.add(2);
        //
        System.out.println("......add "+setDemo);
        setDemo.remove(2);
        //
        System.out.println("......contains "+setDemo.contains(2));
        System.out.println("......size "+setDemo.size());
        System.out.println("......isEmpty "+setDemo.isEmpty());
        setDemo.clear();
        setDemo.add(null);
        System.out.println("......size "+setDemo.size());
        System.out.println("......isEmpty "+setDemo.isEmpty());
        setDemo.remove(2);
        //
        Integer[] a=new Integer[]{2,23,2};
        //list-set
        System.out.println("......containsAll "+setDemo.containsAll(Arrays.asList(a)));
        System.out.println("......addAll "+setDemo.addAll(Arrays.asList(a)));
        System.out.println("......containsAll "+setDemo.containsAll(Arrays.asList(a)));
        for (Integer d: setDemo){
            System.out.println(d);
        }
        //打印没有重写equals和hashcode方法对象不会自动去重，需要重写equals和hashcode方法
        HashSet<QSet> setQSet=new HashSet<>();
        setQSet.add(new QSet());
        setQSet.add(new QSet());
        setQSet.add(new QSet());
        for (QSet Q:setQSet){
            System.out.println(Q);
        }
        //重写equals和hashcode方法的集合
        HashSet<QSetReq> setQSetReq=new HashSet<>();
        setQSetReq.add(new QSetReq("a"));
        setQSetReq.add(new QSetReq("2"));
        setQSetReq.add(new QSetReq("a"));
        for (QSetReq Q:setQSetReq){
            System.out.println(Q);
        }
        //遍历Iterator
        Iterator<QSetReq> setIterator=setQSetReq.iterator();
        while (setIterator.hasNext()){
            System.out.println(setIterator.next());
        }
        Spliterator<QSetReq> spliterator=setQSetReq.spliterator();
        System.out.println("characteristics:"+spliterator.characteristics());
        Spliterator<QSetReq> spliterator2 = spliterator.trySplit();
        //遍历方式1不行，此方式用于先分割
        while(spliterator.tryAdvance(t -> System.out.println("tryAdvance:"+t.toString())));
        //遍历方式2
        while(spliterator2.tryAdvance(t -> System.out.println("trySplit:"+t.toString())));
        System.out.println("characteristics:"+spliterator.characteristics());
        //遍历方式3
        setQSetReq.spliterator().forEachRemaining(t -> System.out.println("forEachRemaining:"+t.toString()));

    }
}
//没有重写equals和hashCode
class QSet{

}
class QSetReq{
    private String username;
    QSetReq(String username){
        this.username=username;
    }

    @Override
    public boolean equals(Object obj) {
        if(obj==null){
            return false;
        }
        if(obj==this){
            return true;
        }
        if(obj instanceof QSetReq){
            QSetReq q2= (QSetReq) obj;
            if(q2.username.equals(this.username)){
                return true;
            }
            return false;
        }
        return false;
    }

    @Override
    public int hashCode() {
        return this.username.hashCode();
    }
}
